<template>
  <el-row :gutter="20">
    <el-col :span="8" :offset="8">
  <div>
    <h3 align="center">登录</h3>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="ruleForm.username"  autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
<!--      舍弃不需要-->
<!--      <el-form-item label="年龄" prop="age">-->
<!--        <el-input v-model.number="ruleForm.age"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
    </el-col>
  </el-row>
</template>

<script>
import axios from "axios";

export default {
  name: "LoginView",
  data() {
  //   舍弃
  //   var checkAge = (rule, value, callback) => {
  //     if (!value) {
  //       return callback(new Error('年龄不能为空'));
  //     }
  //     setTimeout(() => {
  //       if (!Number.isInteger(value)) {
  //         callback(new Error('请输入数字值'));
  //       } else {
  //         if (value < 18) {
  //           callback(new Error('必须年满18岁'));
  //         } else {
  //           callback();
  //         }
  //       }
  //     }, 1000);
  // };

    // 判断登入用户名是否为空
    //rule 规则 value值 callback 回收
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名：'));
      } else {
        callback();
      }
    };

    // 判断登入密码是否为空
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码：'));
      }  else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        password: '',
        // 舍弃
        // age: ''
      },
      //规则校验器：
      rules: {
        username: [
            //validator 验证器
          // validatePass 自定义校验规则
          // trigger 触发器   'blur' 失去焦点
          {validator: validatePass, trigger: 'blur'}
        ],
        password: [
          {validator: validatePass2, trigger: 'blur'}
        ],

        // 舍弃
        // age: [
        //   {validator: checkAge, trigger: 'blur'}
        // ]
      }
    };
  },

  methods: {
    //登入按钮校验器
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('/user/login',this.ruleForm).then((res)=>{
            if(res.data.code = 200){
              //登录成功弹窗
              this.$message({
                message: '恭喜你，登录成功',
                type: 'success',
                duration:1000
              });
              this.$store.commit("storeUser",res.data)
              console.log(res.data)
              //编程式路由
              this.$router.push("/admin")

            }else {
              //失败成功弹窗
              this.$message.error('密码或用户名错误！');

            }
          }).catch((error)=>{
            console.log( +error)
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  }
}
</script>

<style scoped>

</style>